﻿// Wade Arnold: 1/16/2008
// Example is designed to show how to use amfphp to populate a fl.controls.datagrid. 
// TODO: Add method to update the dataset array. 
// TODO: Add an additional call that shows how to get data from a database. 
// TODO: Add a method to update the database dataset. 

package {
	// required for flash file and output display
	import flash.display.MovieClip;
	import fl.events.*;
	import flash.events.*;
	// required to send/recieve data over AMF
	import flash.net.NetConnection;
	import flash.net.Responder;
	// required for the datagrid
	import fl.controls.DataGrid;
	//import fl.controls.dataGridClasses.DataGridColumn;
	import fl.data.DataProvider;

	import org.swxformat.*;
	import flash.text.TextField;
	import fl.controls.Button;
	import flash.utils.*;

	// Flash CS3 Document Class. 
	public class DataGridBenchmark extends MovieClip {
		private var connection:NetConnection;
		private var responder:Responder;

		// Dataprovider for the DataGrid
		private var dp:DataProvider;

		//private var DG_data:DataGrid;
		//private var tf:TextField;
		//private var b1:Button;
		//private var b2:Button;

		private var swx:SWX;

		private var iniTime:int;

		public function DataGridBenchmark() {
			// Create the new dataprovider
			dp = new DataProvider();
			DG_data.dataProvider = dp;
			b1.addEventListener(MouseEvent.MOUSE_DOWN, AMFTest);
			b2.addEventListener(MouseEvent.MOUSE_DOWN, SWXTest);
		}
		
		public function SWXTest(event:*) {
			dp.removeAll();
			iniTime = getTimer();
			tf.text = "SWX Call...";

			swx = new SWX();
			swx.gateway = "http://localhost/swxphp/php/swx.php";
			swx.encoding = "POST";
			swx.timeout = 60;
			swx.debug = false;
			swx.timeoutHandler = timeoutHandler;
			swx.faultHandler = faultHandler;

			swx.DataGrid.getStaticData([], resultHandler);
		}
		
		public function resultHandler(event:Object) {
			tf.text = "Time: "+(getTimer()-iniTime)+"ms Rows: "+event.result.length;
			dp.addItems(event.result);
		}
		
		public function timeoutHandler(event:Object) {
			tf.text = "SWX Call timeout.";
		}
		
		public function faultHandler(event:Object) {
			tf.text = "SWX Call fault.";
		}
		
		public function AMFTest(event:*) {
			dp.removeAll();
			iniTime = getTimer();
			tf.text = "AMF Call...";

			// Responder to handle data returned from AMFPHP.
			responder = new Responder(onResult, onFault);
			connection = new NetConnection;

			// Gateway.php url for NetConnection
			connection.connect("http://localhost/swxphp/php/amf.php");
			// Ask for the data on the load of the flash file. 
			connection.call("DataGrid.getStaticData", responder);
		}

		// Handle a successful AMF call. This method is defined by the responder. 
		private function onResult(result:Object):void {
			tf.text = "Time: "+(getTimer()-iniTime)+"ms Rows: "+result.length;
			dp.addItems(result);
		}
		
		// Handle an unsuccessfull AMF call. This is method is dedined by the responder. 
		private function onFault(fault:Object):void {
			trace(String(fault.description));
			tf.text = "AMF Call fault.";
		}
	}
}